package com.webware.view.client.presenter;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.webware.view.client.ClientFactory;
import com.webware.view.client.event.ChangeSearchStringEvent;
import com.webware.view.client.event.DoSearchEvent;
import com.webware.view.client.place.StartPlace;
import com.webware.view.client.ui.HeaderView;

public class HeaderPresenter extends AbstractActivity implements Presenter {

    private ClientFactory clientFactory;

    public HeaderPresenter(StartPlace place, ClientFactory clientFactory) {
	this.clientFactory = clientFactory;
    }

    public void start(AcceptsOneWidget panel, EventBus eventBus) {
	HeaderView view = clientFactory.getHeaderView();
	view.setPresenter(this);
	panel.setWidget(view.asWidget());

	view.getSubmitButton().addClickHandler(new ClickHandler() {
	    public void onClick(ClickEvent event) {
		clientFactory.getEventBus().fireEvent(new DoSearchEvent());
	    }
	});

	view.getSearchBox().addValueChangeHandler(
		new ValueChangeHandler<String>() {
		    public void onValueChange(ValueChangeEvent<String> event) {
			clientFactory.getEventBus().fireEvent(
				new ChangeSearchStringEvent());
		    }
		});
    }

    public void goTo(Place place) {
	clientFactory.getPlaceController().goTo(place);
    }

}
